<?php
/**
 * 本类主要完成小程序授权方面逻辑
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/10/22
 * Time: 09:40
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\service\MiniEvent;
use app\api\service\MiniHandle;
use app\api\validate\IDMustBePositiveInt;
use app\api\validate\MiniAuthParameter;
use app\api\validate\MiniEventParameter;
use app\api\validate\MiniParameter;
use app\api\service\Mini as MiniService;
use app\lib\exception\MiniSuiteException;
use app\api\service\Token;
use app\lib\exception\SuccessMessage;
use think\Exception;

class Mini extends BaseController
{
    /**
     * 用于返微信第三方平台的通信标识，验证url的有效性接收get请求
     * 以及接收微信返回的post请求。获取数据
     * 此应用没有权限限制，对外的公共方法。用于接收微信推送的suite_ticket通知
     * @return string
     * @throws Exception
     * @throws MiniSuiteException
     */
    public function getSuite(){
        $notify = new MiniService();
        (new MiniParameter())->goCheck();
        $sEchoStr =  $notify->getNotify();
        if (is_array($sEchoStr)){
            return 'success';
        }else{
            throw new MiniSuiteException();
        }
    }

    /**
     * 获取授权url
     * @return string
     * @throws \app\lib\exception\TokenException
     */
    public function getAuthUrl(){
        $token = Token::getCurrentTokenValue();
        $contact = new MiniService();
        $url =  $contact->getAuthUrl($token);
        return $url;
    }
    /**
     * 获取授权码，并获取授权信息
     */
    public function receiveUri(){
        (new MiniAuthParameter())->goCheck();
        $data = input('post.');
        $userInfo =Token::getCurrentToken($data['token']);
        $t_id = $userInfo['uid'];
        $mini = new MiniService();
        $result = $mini->getUriData($t_id,$data);
        if ($result){
            //设置小程序服务信息
            $mini = new MiniHandle();
            $step1 =  $mini->setServerUrl($t_id);//设置服务器域名
            $step2 =  $mini->setWebViewUrl($t_id);//设置业务域名
            if ($step1 && $step2 ){
                return json(new SuccessMessage(),201);
            }
        }else{
            $data = [
                'code'=>400,
                'msg'=>'获取授权信息失败',
                'error_code'=>'61000'
            ];
            return json($data);
        }
    }

    /**
     * 接收小程序事件与消息处理
     * @return string
     * @throws MiniSuiteException
     */
    public function getEventInfo($appid){
        //error_log(print_r($appid, 1), 3, "./userinfo");
        $notify = new MiniEvent();
        (new MiniEventParameter())->goCheck();
        $sEchoStr =  $notify->getEventInfo($appid);
        if (is_array($sEchoStr)){
            return 'success';
        }else{
            throw new MiniSuiteException();
        }
    }

    /**
     * 获取小程序的access_token(测试)
     * @param $appid
     */
    public function getAccessToken($appid){
        $mini = new MiniService();
        $uid = Token::getCurrentUid();
        $result = $mini->getAccessToken($appid);
        var_export($result);
        exit();
    }

    /**
     * 获取小程序草稿列表
     */
    public function getDraftList()
    {
        $mini = new MiniHandle();
        try{
            $result = $mini->getDraftList();
        }catch (Exception $exception){
            $result['errmsg'] = $exception->getMessage();
            $result['error_code'] = 26001;//获取小程序模板失败
        }
        return json($result);
    }

    /**
     * 获取小程序模板列表
     */
    public function getTemplateList(){
        $mini = new MiniHandle();
        try{
            $result = $mini->getTemplateList();
        }catch (Exception $exception){
            $result['errmsg'] = $exception->getMessage();
            $result['error_code'] = 26002;//获取小程序模板失败
        }
        return json($result);
    }
    /**
     * 设置下程序模板
     */
    public function addTemplate($id){
        (new IDMustBePositiveInt())->goCheck();
        $mini = new MiniHandle();
        try{
            $result = $mini->addToTemplate($id);
        }catch (Exception $exception){
            $result['errmsg'] = $exception->getMessage();
            $result['error_code'] = 26003;//获取小程序模板失败
        }
        return json($result);
    }

    /**
     * 删除小程序模板
     * @param $id
     * @return \think\response\Json
     */
    public function deleteTemplate($id){
        (new IDMustBePositiveInt())->goCheck();
        $mini = new MiniHandle();
        try{
            $result = $mini->deleteTemplate($id);
        }catch (Exception $exception){
            $result['errmsg'] = $exception->getMessage();
            $result['error_code'] = 26004;//获取小程序模板失败
        }
        return json($result);
    }
    /**
     *设置小程序服务器域名(测试)
     */
    public function setMiniUrl(){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $result =  $mini->setServerUrl($uid);
        }catch (Exception $exception){
            $result['errmsg'] = $exception->getMessage();
            $result['error_code'] = 26005;//设置小程序域名
        }
        return json($result);
    }

    /**
     * 设置小程序业务域名
     * @return \think\response\Json
     */
    public function setWebViewUrl(){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $result =  $mini->setWebViewUrl($uid);
        }catch (\Exception $exception){
            $result['errmsg'] = $exception->getMessage();
            $result['error_code'] = 26006;//设置小程序域名
        }
        return json($result);
    }
    /**
     * 绑定体验者
     */
    public function bindMember($wechatId){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
       try{
            $result =  $mini->bindMember($uid,$wechatId);
        }catch (\Exception $exception){
            $result['errmsg'] = $exception->msg;
            $result['error_code'] = 26007;//绑定小程序体验者
        }
        return json($result);
    }
    /**
     * 解绑体验者
     */
    public function unBindMember($wechatId){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $result =  $mini->unBindMember($uid,$wechatId);
        }catch (\Exception $exception){
            $result['errmsg'] = $exception->msg;
            $result['error_code'] = 26008;//解绑体验者失败
        }
        return json($result);
    }
    /**
     * 所有体验者
     */
    public function lsitMemeber(){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $result =  $mini->listMember($uid);
        }catch (\Exception $exception){
            $result['errmsg'] = $exception->msg;
            $result['error_code'] = 26009;//小程序所有体验者
        }
        return json($result);
    }

    /**
     * 提交代码
     * @param $template_id
     * @param $user_version
     * @param $desc
     * @return \think\response\Json
     */
    public function codeSubmit($template_id,$user_version,$desc){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $result =  $mini->codeSubmit($uid,$template_id,$user_version,$desc);
        }catch (\Exception $exception){
            $result['errmsg'] = $exception->msg;
            $result['error_code'] = 26009;//提交小程序代码审核
        }
        return json($result);

    }
    /**
     * 小程序体验二维码
     * @return \think\response\Json
     */
    public function getQrcode(){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $result =  $mini->getExpVersion($uid);
        }catch (\Exception $exception){
            $result['errmsg'] = $exception->msg;
            $result['error_code'] = 26009;//获取体验二维码
        }
        return json($result);
    }
    /**
     * 代码提交审核
     */
    public function submitReview($tag){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $result = $mini->submitReview($uid,$tag);
        }catch (\Exception $exception){
            $result['errmsg'] = $exception;
            $result['error_code'] = 26010;//代码提交审核
        }
        return json($result);
    }
    /**
     * 获取特定版本的下程序代码审核状态
     */
    public function getAuditStatus($auditid){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $result = $mini->getAuditStatus($uid,$auditid);
        }catch (\Exception $exception){
            $result['errmsg'] = $exception->msg;
            $result['error_code'] = 26011;//获取指定审核版本的小程序查询状态
        }
        return json($result);
    }
    /**
     * 查询最近一次提交代码的审核的状态
     */
    public function getLatestAudit(){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $result = $mini->getLatestAudit($uid);
        }catch (Exception $exception){
            $result['errmsg'] = $exception->msg;
            $result['error_code'] = 26012;//获取最近一次提交的审核状态
        }
        return json($result);
    }
    /**
     * 撤销改小程序的审核
     */
    public function unDocodeAudit(){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $result = $mini->unDocodeAudit($uid);
        }catch (Exception $exception){
            $result['errmsg'] = $exception->msg;
            $result['error_code'] = 26013;//小程序撤销审核
        }
        return json($result);
    }
    /**
     * 发布已审核的小程序
     */
    public function release(){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $result = $mini->release($uid);
        }catch (Exception $exception){
            $result['errmsg'] = $exception->msg;
            $result['error_code'] = 26014;//发布已审核的小程序
        }
        return json($result);
    }
    /**
     * 解绑小程序
     * @return \think\response\Json
     */
    public function unbundAuthor(){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        $result = $mini->unbundAuthor($uid);
        return json($result);
    }
    /**
     * 获取小程序消息模板列表
     * @param $page
     * @return \think\response\Json
     */
    public function getAllTemplate($page){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        $result = $mini->getAllTemplate($uid,$page);
        return json($result);
    }

    /**
     * 获取小程序消息模板详情
     * @param $id
     * @return \think\response\Json
     */
    public function getTemplateDetail($id){
        $uid = Token::getCurrentUid();
        $mini = new MiniHandle();
        $result = $mini->getTemplateDetail($uid,$id);
        return json($result);
    }

    /**
     * 添加小程序模板消息
     * @param $id
     * @param $keyword_list
     * @return \think\response\Json
     */
    public function addWxopenTemplate($id,$keyword_list){
        $t_id = Token::getCurrentUid();
        $mini = new MiniHandle();
        try{
            $data = $mini->addTemplate($t_id,$id,$keyword_list);
            $result['errmsg'] = '';
            $result['error_code'] = 0;//发布已审核的小程序
            $result['template_id'] = $data;
        }catch (Exception $exception){
            $result['errmsg'] = $exception->msg;
            $result['error_code'] = 26014;//发布已审核的小程序
        }
        return json($result);
    }

}